package cn.bqjr.eily.samples.common.shiro;

import cn.bqjr.eily.core.entity.SysProperties;
import cn.bqjr.eily.samples.utils.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 踢除用户 异步任务
 */
@Component
public class KickoutUserAsync {

    private static final Logger logger = LoggerFactory.getLogger(KickoutUserAsync.class);

    @Autowired
    private SysProperties sysProperties;

    /**
     * 踢除指定用户
     *
     * @param userId
     */
    @Async
    public void kickoutByUserId(Long userId) {
        try {
            logger.debug("kickoutByUserId start...");
            ShiroUtils.deleteSessionByUserId(userId);
            logger.debug("kickoutByUserId end...");
        } catch (Exception e) {
            logger.error("kickoutByUserId error", e);
        }
    }

    /**
     * 剔除指定用户
     *
     * @param loginId
     */
    @Async
    public void kickoutByLoginId(String loginId, String companyId) {
        try {
            logger.debug("kickoutByLoginId start...");
            ShiroUtils.deleteSessionByLoginId(loginId, companyId);
            logger.debug("kickoutByLoginId end...");
        } catch (Exception e) {
            logger.error("kickoutByLoginId error", e);
        }
    }

    /**
     * 剔除同帐号的其他用户
     */
    @Async
    public void kickoutOtherSameUser() {
        try {
            if (!sysProperties.getEnabledMutliUserLogin()) {
                logger.debug("kickoutOtherSameUser start...");
                ShiroUtils.kickoutOtherSameUser();
                logger.debug("kickoutOtherSameUser end...");
            }
        } catch (Exception e) {
            logger.error("kickoutOtherSameUser error", e);
        }
    }
}
